//
//  ListCell.m
//  CarLifes
//
//  Created by yu on 2017/6/30.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "ListCell.h"

#import "EquipmentModel.h"

@interface ListCell ()

@end

@implementation ListCell

- (void)awakeFromNib {
    [super awakeFromNib];
 
    UIView *contentView = self.contentView;
    
    UIView *lineView = [[UIView alloc] init];
    
    [contentView addSubview:lineView];
    if (@available(iOS 13.0, *)) {
        lineView.backgroundColor = UIColor.separatorColor;
    } else {
        lineView.backgroundColor = kLineGrayColor;
    }
    
    lineView.sd_layout
    .leftSpaceToView(contentView, 17)
    .bottomEqualToView(contentView)
    .rightEqualToView(contentView)
    .heightIs(0.5);
    
    for (UIButton *btn in self.btns) {
        // 设置button的图片约束
        btn.imageView.sd_layout
        .widthRatioToView(btn, 0.3)
        .topSpaceToView(btn, 13)
        .centerXEqualToView(btn)
        .heightEqualToWidth();
        
        // 设置button的label约束
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        btn.titleLabel.sd_layout
        .topSpaceToView(btn.imageView, 7)
        .centerXEqualToView(btn)
        .heightIs(15)
        .widthRatioToView(btn, 1);
        
        // 设置高度约束
        btn.sd_layout.autoHeightRatio(1.05);
        
        [btn addTarget:self action:@selector(funcAction:) forControlEvents:UIControlEventTouchUpInside];
    }
}
- (IBAction)funcAction:(UIButton *)btn
{
    if (_funcActionBlock) {
        _funcActionBlock(btn.tag-1);
    }
}

- (void)setModel:(EquipmentModel *)model
{
    _model = model;
    
    if ([model.mark isEqualToString:@"D"]) {
        //self.contentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        _nameLabel.textColor = kRgb(240, 240, 240);
        _deviceIdLabel.textColor = kRgb(240, 240, 240);
        _statusLabel.textColor = kRgb(240, 240, 240);
    }
    
    _nameLabel.text = NSStringFormat(@"车主名：%@",model.ownerName?:@"");
    
    _deviceIdLabel.text = NSStringFormat(@"设备号：%@",model.eqNo?:@"");
    
    NSString *str = _model.status;
    if ([str isEqualToString:@"3"])
    {//未启用3
        _deviceImgView.image = IMAGE_NAMED(@"unactivated_device");
        _statusLabel.text = @"未启用";
        _statusLabel.textColor = kTextBlackColor;
    }
    else if ([str isEqualToString:@"2"])
    {//离线2
        _deviceImgView.image = IMAGE_NAMED(@"offline_device");
        NSString *timeStatus = [_model.cnStatus stringByReplacingOccurrencesOfString:@" " withString:@""];
//        PPLog(@"状态 = %@",_model.cnStatus);
        if (![timeStatus containsString:@"静止"]) {
            _statusLabel.textColor = [UIColor redColor];
        }
        _statusLabel.text = [timeStatus substringToIndex:2];
    }
    else
    {//在线0,1
        _deviceImgView.image = IMAGE_NAMED(@"online_device");
        NSString *timeStatus = [_model.cnStatus stringByReplacingOccurrencesOfString:@" " withString:@""];
        _statusLabel.text = [timeStatus substringToIndex:2];
    }
}

+ (instancetype)initTableViewCellWith:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
    NSString *identifier;
    NSInteger index = 0;
    switch (indexPath.row) {
        case 0:
            identifier = @"ListCell0";
            index = 0;
            break;
        case 1:
            identifier = @"ListCell1";
            index = 1;
            break;
        default:
            break;
    }
    ListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ListCell" owner:self options:nil] objectAtIndex:index];
    }
    return cell;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end


